package edu.gatech.oad;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class CreateItemScreen extends Activity {

	EditText location;
	EditText date;
	EditText description;
	String category;
	private String itemStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_item_screen);
		final Button cancelItemB = (Button) findViewById(R.id.CancelButton);
		final Button acceptButtonB = (Button) findViewById(R.id.AcceptButton);
		final Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
		final EditText location = (EditText) findViewById(R.id.editLocation);
		final EditText date = (EditText) findViewById(R.id.editDate);
		final EditText description = (EditText) findViewById(R.id.editDescription);
		final RadioButton rbtnLost = (RadioButton) findViewById(R.id.radioLost);
		itemStatus = "";

		// Category Buttons
		final RadioButton booksCategory = (RadioButton) findViewById(R.id.booksRadio);
		final RadioButton electronicsCategory = (RadioButton) findViewById(R.id.electronicsRadio);
		final RadioButton furnitureCategory = (RadioButton) findViewById(R.id.furnitureRadio);
		final RadioButton preciousCategory = (RadioButton) findViewById(R.id.preciousRadio);
		
		final int PICTURE_ACTIVITY = 1;

		acceptButtonB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DataBaseHelper dbHelper = new DataBaseHelper(getApplicationContext());

				try {
					dbHelper.createDataBase();
					dbHelper.openDataBase();
				} catch (Exception sqle) {
					throw new Error("Unable to create database");
				}

				String status = "Found";
				if (rbtnLost.isChecked()) {
					status = "Lost";
				}

				String category;
				if (booksCategory.isChecked()) {
					category = "Books";
				} else if (electronicsCategory.isChecked()) {
					category = "Electronics";
				} else if (furnitureCategory.isChecked()) {
					category = "Furniture";
				} else if (preciousCategory.isChecked()) {
					category = "Precious Items";
				} else {
					category = "Other";
				}

				dbHelper.addItem(status, category, description.getText().toString(), location.getText().toString(),
						date.getText().toString(), LoginActivity.gblCurrentUser);
				Intent a = new Intent(CreateItemScreen.this, ItemList.class);
				a.putExtra("status", itemStatus);
				startActivity(a);
			}
		});
		cancelItemB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				Intent b = new Intent(CreateItemScreen.this, MainActivity.class);
				startActivity(b);

			}
		});
		
		btnTakePicture.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Normally you would populate this with your custom intent.
				startActivityForResult(cameraIntent, PICTURE_ACTIVITY);
			}
		});
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode == RESULT_CANCELED) {
			System.out.println("CreatItemScreen : User did not take picture");
		} else {
			//get picture from activity and save to DB
			Bundle extras = intent.getExtras();
			Bitmap imageBitmap = (Bitmap) extras.get("data");
			// DB stuff here. also need to add some imageviews to the item
			// display screen so you can actually see it
		}
	}
	
	public static boolean isIntentAvailable(Context context, String action) {
	    final PackageManager packageManager = context.getPackageManager();
	    final Intent intent = new Intent(action);
	    List<ResolveInfo> list =
	            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
	    return list.size() > 0;
	}
}